package com.woniuxy.oldmanapi.service;

import com.woniuxy.oldmanapi.cvo.CareCareprojectVo;
import com.woniuxy.oldmanapi.cvo.CareVo;
import com.woniuxy.oldmanapi.entity.Care;
import com.woniuxy.oldmanapi.entity.Older;
import com.woniuxy.oldmanapi.mapper.CareCareprojectMapper;
import com.woniuxy.oldmanapi.mapper.CareMapper;
import com.woniuxy.oldmanapi.mapper.OlderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CareService {
    @Autowired
    CareMapper careMapper;
    @Autowired
    CareCareprojectMapper careCareprojectMapper;
    @Autowired
    OlderMapper olderMapper;

    public List<Care> selectAllCare() {
        List<Care> cares = careMapper.selectAll();
        return cares;
    }

    public List<CareVo> findCareVoByCareId(int careId) {
       return careMapper.selectByCareId(careId);
    }

    //删除=判断是否被使用-->删除护理等级，删除明细
    @Transactional
    public void removeByCareId(int id) {
     List<Older> olders=olderMapper.selectByCareId(id);
         if(olders.size()>0){
             throw  new RuntimeException("此护理级别有长者正接受服务，不可删除！");
         }else {
             careMapper.deleteByPrimaryKey(id);
             careCareprojectMapper.removeByCareid(id);
         }
    }

    //添加==添加护理级别，添加明细中间表
    @Transactional
    public void addCare(CareVo careVo) {
        Care care=new Care();
        care.setCprice(careVo.getCprice());
        care.setExplained(careVo.getExplained());
        care.setLevel(careVo.getLevel());
        careMapper.insert(care);

        List<CareCareprojectVo> careCareprojectVos = careVo.getCareCareprojectVos();
        Integer careId = care.getId();
        careCareprojectMapper.insertBeach(careId,careCareprojectVos);
    }
    //修改==修改护理等级，删除明细，添加明细
    @Transactional
    public void modifyCare(CareVo careVo) {
        Care care=new Care();
        care.setCprice(careVo.getCprice());
        care.setId(careVo.getId());
        care.setExplained(careVo.getExplained());
        care.setLevel(careVo.getLevel());

        careMapper.updateByPrimaryKey(care);

        careCareprojectMapper.removeByCareid(careVo.getId());
        List<CareCareprojectVo> careCareprojectVos = careVo.getCareCareprojectVos();
        Integer careId = careVo.getId();
        careCareprojectMapper.insertBeach(careId,careCareprojectVos);
    }

    public List<Care> findCareAll() {
        return  careMapper.selectAll();
    }

    public CareVo findCareVoByCId(int careId) {
     return    careCareprojectMapper.selectcardvoByCid(careId);
    }

    public List<Care> selectAllListYsw() {
        return careMapper.selectAll();
    }
}
